﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Gds.Utility.Helper
{
    public static class PublicHelper
    {
        public static event EventHandler ShowMsgEvent;
        public static void ShowLogMsg(string msg)
        {
            if (ShowMsgEvent != null)
            { 
            ShowMsgEvent(msg, new EventArgs());

            }
        }
        public static void WriteMsg(string msg)
        {
            try
            {
                string logFormat = $"[{DateTime.Now.ToString("yyyyMMdd HH:mm:ss")}] {msg}\r\n";
                if (!Directory.Exists(Utility.Const.GbUnit.LogPath)) Directory.CreateDirectory(Utility.Const.GbUnit.LogPath);

                string path = Path.Combine(Utility.Const.GbUnit.LogPath, $"{DateTime.Now.ToString("yyyyMMdd")}.txt");
                System.IO.File.AppendAllText(path, logFormat);
            }
            catch
            {

            }
        }
        public static void WriteControllerMsg(string msg,int _totalLine, int _controller)
        {
            try
            {
                string logFormat = $"[{DateTime.Now.ToString("yyyyMMdd HH:mm:ss")}] {msg}\r\n";
                if (!Directory.Exists(Utility.Const.GbUnit.ControllerLogPath+DateTime.Now.ToString("yyyy_MM_dd")+"\\")) Directory.CreateDirectory(Utility.Const.GbUnit.ControllerLogPath + DateTime.Now.ToString("yyyy_MM_dd") + "\\");

                string path = Path.Combine((Utility.Const.GbUnit.ControllerLogPath + DateTime.Now.ToString("yyyy_MM_dd") + "\\"), $"总线-{_totalLine}-控制器-{_controller}.txt");
                System.IO.File.AppendAllText(path, logFormat);
            }
            catch
            {

            }
        }
        public static void WriteErrorMsg(string msg)
        {
            try
            {
                string logFormat = $"[{DateTime.Now.ToString("yyyyMMdd HH:mm:ss")}] {msg}\r\n";

                if (!Directory.Exists(Utility.Const.GbUnit.ErrorPath)) Directory.CreateDirectory(Utility.Const.GbUnit.ErrorPath);

                string path = Path.Combine(Utility.Const.GbUnit.ErrorPath, $"{DateTime.Now.ToString("yyyyMMdd")}.txt");
                System.IO.File.AppendAllText(path, logFormat);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public static string MD5Encrypt32(string password)
        {
            string cl = password;
            string pwd = "";
            MD5 md5 = MD5.Create(); //实例化一个md5对像
                                    // 加密后是一个字节类型的数组，这里要注意编码UTF8/Unicode等的选择　
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
            // 通过使用循环，将字节类型的数组转换为字符串，此字符串是常规字符格式化所得
            for (int i = 0; i < s.Length; i++)
            {
                // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母，如果使用大写（X）则格式后的字符是大写字符 
                pwd = pwd + s[i].ToString("X");
            }
            return pwd;
        }
        public static void WriteErrorMsg(Exception exception, int pos = 0)
        {
            try
            {
                string logFormat = $"[{DateTime.Now.ToString("yyyyMMdd HH:mm:ss")}] {exception.Message}\r\n{exception.StackTrace}\r\n";

                if (!Directory.Exists(Utility.Const.GbUnit.ErrorPath)) Directory.CreateDirectory(Utility.Const.GbUnit.ErrorPath);

                string path = Path.Combine(Utility.Const.GbUnit.ErrorPath, $"{DateTime.Now.ToString("yyyyMMdd")}.txt");
                System.IO.File.AppendAllText(path, logFormat);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public static void WriteErrorMsg(Exception exception, string FunctionType)
        {
            try
            {
                string logFormat = $"[{DateTime.Now.ToString("yyyyMMdd HH:mm:ss")}] {exception.Message}\r\n{exception.StackTrace}\r\n";
                logFormat = "[" + FunctionType + "]=>" + logFormat;
                if (!Directory.Exists(Utility.Const.GbUnit.ErrorPath)) Directory.CreateDirectory(Utility.Const.GbUnit.ErrorPath);

                string path = Path.Combine(Utility.Const.GbUnit.ErrorPath, $"{DateTime.Now.ToString("yyyyMMdd")}.txt");
                System.IO.File.AppendAllText(path, logFormat);
            }
            catch (Exception ex)
            {
                Utility.Helper.PublicHelper.WriteErrorMsg(ex);
            }
        }
    }
}
